<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * bookhistory_model
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class Bookinfobill_model extends MY_Model{
	
	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_infobill';
	}
        
        public function updateInfo($params){
            $sql = "UPDATE {$this->table_name} SET fare = {$params->fare},infoBill='{$params->infoBill}',
                save_contact={$params->save_contact},agent_code={$params->agent_code},owner={$params->owner},lastupdated='{$params->lastupdated}'
                WHERE customerID='{$params->customerID}' AND owner = {$params->owner}";              
//die($sql);
            $this->db->query($sql);
            return $this->db->affected_rows();
        }

        public function init_data($right){
		return array(
                                array(
						'name' => 'id',
						'label' => $this->lang->line('number'),
						'width' => '2%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),
                                array(
						'name' => 'name',
						'label' => $this->lang->line('name'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),
				array(
						'name' => 'booking_place_code',
						'label' => $this->lang->line('booking_place_code'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'airline',
						'label' => $this->lang->line('airline'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),
				array(
						'name' 	=> 'payment_method',
						'label' => $this->lang->line('payment_method'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),
                                array(
						'name' 	=> 'net_fare',
						'label' => $this->lang->line('net_fare'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),array(
						'name' 	=> 'real_fare',
						'label' => $this->lang->line('real_fare'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),
                                array(
						'name' 	=> 'payment_confirm',
						'label' => $this->lang->line('payment_confirm'),
						'width' => '10%',
						'sort'  => 'DESC',
						'searchoptions' => FALSE
				),
                                array(
						'name' 	=> 'owner',
						'label' => $this->lang->line('booker'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => FALSE
				),
                                array(
						'name' 	=> 'lastupdated',
						'label' => $this->lang->line('lastupdated'),
						'width' => '5%',
						'sort'  => 'DESC',
						'searchoptions' => FALSE
				),
                                array(
						'name'  => 'button',
						'width' => '5%',
						'sort'  => FALSE,
						'label'  => false,
						'searchoptions' => false
				)
		);
	}
	
	public function json_data($controller, $right){
                if($this->session->userdata('time')){
                    $time = json_decode($this->session->userdata('time'));
                }
                
                if($this->session->userdata('customerID')){
                    $customerID = json_decode($this->session->userdata('customerID'));
                }
                
                if($this->session->userdata('userid')){
                    $userid = json_decode($this->session->userdata('userid'));
                }
                
                if($this->session->userdata('payment_method')){
                    $payment_method = json_decode($this->session->userdata('payment_method'));
                }
                
		$this->datatables
		->select("tbl.id,tbl.customerID,tbl.booking_place_code, tbl.airline, tbl.payment_air, tbl.lastupdated, tbl.createdTime, tbl.net_fare, tbl.real_fare, med.payment_name,usr.username,cts.name")
		->from($this->table_name . ' AS tbl')
                ->join('book_payment_method AS med','med.id=tbl.payment_method','inner')
                ->join('users AS usr','usr.id=tbl.owner','inner')
                ->join('book_contacts AS cts','cts.customerID=tbl.customerID','inner');
	
                if(isset($time) && $time->beginTime != ''){
                    $this->datatables->where('tbl.createdTime >= '.strtotime($time->beginTime));
                }
                
                if(isset($time) && $time->endTime != ''){
                    $this->datatables->where('tbl.createdTime <= '.strtotime($time->endTime.' 24:00:00'));
                }
                
                if(isset($customerID) && $customerID->customerID != ''){
                    $this->datatables->where('tbl.customerID',$customerID->customerID);
                }
                
                if(isset($userid) && $userid->userid != ''){
                    $this->datatables->where('tbl.owner',$userid->userid);
                }
                
                if(isset($payment_method) && $payment_method->payment_method != ''){
                    $this->datatables->where('tbl.payment_method',$payment_method->payment_method);
                }
                
		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
                $this->session->unset_userdata('time');
                $this->session->unset_userdata('customerID');
                $this->session->unset_userdata('userid');
                $this->session->unset_userdata('payment_method');
                
		$ouput['aaData'] = array();
                $count = 1;
                
		foreach($datatables['aaData'] as $item){
			$ouput['aaData'][] = array(
					$count++,
                                        $item['name'],
					$item['booking_place_code'],
					$item['airline'],
                                        $item['payment_name'],
                                        $this->bookinglib->my_number_format($item['net_fare'], 2, ',', ','),
                                        $this->bookinglib->my_number_format($item['real_fare'], 2, ',', ','),
                                        ($item['payment_air'] == 1)?'OK':'',
                                        $item['username'],
                                        date("d-m-Y H:i:s",$item['lastupdated']),
					$this->add_button_history($controller, $right, $item['customerID']),
			);
		}
                
	
		return json_encode($ouput);
	}
}